from typing import Dict, Optional, Type
from .base import BaseDownloader
from .youtube import YouTubeDownloader
from .douyin import DouyinDownloader

class DownloaderFactory:
    _downloaders: Dict[str, Type[BaseDownloader]] = {
        "youtube": YouTubeDownloader,
        "douyin": DouyinDownloader
    }
    
    @classmethod
    def get_downloader(cls, url: str, config) -> Optional[BaseDownloader]:
        """根据URL获取对应的下载器实例"""
        # 判断URL类型
        if "youtube.com" in url or "youtu.be" in url:
            downloader_class = cls._downloaders.get("youtube")
        elif "douyin.com" in url:
            downloader_class = cls._downloaders.get("douyin")
        else:
            return None
            
        if downloader_class:
            return downloader_class(config)
        return None
    
    @classmethod
    def register_downloader(cls, name: str, downloader_class: Type[BaseDownloader]):
        """注册新的下载器类型"""
        cls._downloaders[name] = downloader_class
    
    @classmethod
    def get_supported_downloaders(cls) -> Dict[str, Type[BaseDownloader]]:
        """获取所有支持的下载器类型"""
        return cls._downloaders.copy() 